#include "stdafx.h"
#include "DIB24.h"

CDIB24::CDIB24()
{
	m_pData = NULL;
	updateBitmapHeader(CSize(0, 0));
}

CDIB24::CDIB24(const CSize &size)
{
	m_pData = NULL;
	updateBitmapHeader(size);
}

CDIB24::~CDIB24()
{
	releaseMemory();
}

void CDIB24::allocMemory()
{
	releaseMemory();

	m_pData = new BYTE[m_bitmHeader.biWidth*m_bitmHeader.biHeight*4];
}

void CDIB24::releaseMemory()
{
	if(m_pData != NULL)
	{
		delete[] m_pData;
		m_pData = NULL;
	}
}

bool CDIB24::isMemAllocated() const
{
	return (m_pData != NULL);
}

int	CDIB24::getWidth() const
{
	return m_bitmHeader.biWidth;
}

int CDIB24::getHeight() const
{
	return m_bitmHeader.biHeight;
}

CSize CDIB24::getSize() const
{
	return CSize(m_bitmHeader.biWidth, m_bitmHeader.biHeight);
}

const BYTE* CDIB24::getData() const
{
	return m_pData;
}

BYTE* CDIB24::getData()
{
	return m_pData;
}

bool CDIB24::loadFromBMP(const std::string &strFName)
{
	//bool res = false;

	//releaseMemory();

	//std::ifstream instream(strFName.c_str());

	//if(instream.is_open())
	//	res = readBMP(instream);
	//else
	//	res = false;

	//return res;
	return false;
}

void CDIB24::draw(CDC *pDC, const CRect &srcRect,
						const CRect &dstRect) const
{
	pDC->SetStretchBltMode(COLORONCOLOR);
	::StretchDIBits(
		pDC->GetSafeHdc(), dstRect.left, dstRect.top, dstRect.Width(), dstRect.Height(),
		srcRect.left, srcRect.top, srcRect.Width(), srcRect.Height(),
		m_pData, (LPBITMAPINFO) &m_bitmHeader, DIB_RGB_COLORS, SRCCOPY);
}

const BITMAPINFOHEADER& CDIB24::getBitmapHeader() const
{
	return m_bitmHeader;
}

BITMAPINFOHEADER& CDIB24::getBitmapHeader()
{
	return m_bitmHeader;
}

void CDIB24::updateBitmapHeader(const CSize& size)
{
	m_bitmHeader.biSize = sizeof(BITMAPINFOHEADER);
	m_bitmHeader.biWidth = size.cx;
	m_bitmHeader.biHeight = size.cy;
	m_bitmHeader.biPlanes = 1;
	m_bitmHeader.biBitCount = 32;
	m_bitmHeader.biCompression = BI_RGB;
	m_bitmHeader.biSizeImage = 0;
	m_bitmHeader.biXPelsPerMeter = 0;
	m_bitmHeader.biYPelsPerMeter = 0;
	m_bitmHeader.biClrUsed = 0;
	m_bitmHeader.biClrImportant = 0;
}

bool CDIB24::readBMP(std::ifstream &istream)
{
	//bool res = true;
	//BITMAPFILEHEADER bmfh;
	//BITMAPINFOHEADER bmih;
	//istream.read((char*) &bmfh, sizeof(BITMAPFILEHEADER));
	//if(istream.fail())
	//	res = false;
	//else
	//	if(bmfh.bfType != 0x4d42)
	//		res = false;
	//if(res)
	//{
	//	int nSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
	//	if(nSize != sizeof(BITMAPINFOHEADER))//Fail if have palette
	//		res = false;
	//	else
	//	{
	//		istream.read((char*) &bmih, sizeof(BITMAPINFOHEADER));
	//		if(istream.fail())
	//			res = false;
	//		else
	//			if(bmih.biBitCount != 24)//Read only 32 bpp images
	//				res = false;
	//			else
	//			{
	//				updateBitmapHeader(CSize(bmih.biWidth, bmih.biHeight));
	//				allocMemory();
	//				int nMemoryNeeded = m_bitmHeader.biWidth*m_bitmHeader.biHeight*3;
	//				istream.read((char*) m_pData, sizeof(BYTE)*nMemoryNeeded);
	//				if(istream.fail())
	//					res = false;
	//			}
	//	}
	//}
	//return res;
	return false;
}